package servlet;

import dao.WishListDao;
import entity.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/Wish")
public class WishListService extends BasicServlet{
    /**
     * 得到当前用户的收藏夹列表
     * @param request
     * @param response
     * @return
     */
    public String wishList(HttpServletRequest request, HttpServletResponse response){
        User user = (User)request.getSession(false).getAttribute("user");
        try {
            if(user == null){
                response.sendRedirect("/page/login");
            }
            request.getSession(false).setAttribute("wishList",WishListDao.getUserWishList(user.getId()));
            response.sendRedirect("/page/wishList");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Success";
    }


    public String wishDelete(HttpServletRequest request, HttpServletResponse response){
        String productId = request.getParameter("productId"),userId = request.getParameter("userId");
        System.out.println(productId + "------->" + userId);
        int result = WishListDao.deleteProduct(productId,userId);
        try {
            if(result <= 0) return "Error";
            System.out.println(result);
            request.getSession(false).setAttribute("wishList",WishListDao.getUserWishList(Integer.parseInt(userId)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Success";
    }
}
